package com.zl.tby.core.base.util;

import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author changpeng
 * @version 1.0
 * @since 2024/11/4 16:23
 */
@Slf4j
public class EcologySocket9002 {
    public static void start() {
        new Thread(() -> {
            try {
                int port = 9001;
                ServerSocket serverSocket = new ServerSocket(port);
                while (true) {
                    if (serverSocket.isBound() && !serverSocket.isClosed()) {
                        System.out.println(port + ": ServerSocket已连接");
                    } else {
                        System.out.println(port + ": ServerSocket已关闭 正在重新打开链接");
                        serverSocket = new ServerSocket(port);
                    }
                    Socket clientSocket = serverSocket.accept();
                    if (clientSocket.isConnected()) {
                        System.out.println(port + ": 客户端已连接");
                    } else {
                        System.out.println(port + ": 客户端连接失败");
                        serverSocket.close();
                        serverSocket = new ServerSocket(port);
                    }
                    /*try (InputStream input = clientSocket.getInputStream()) {
                        DataInputStream dataInputStream = new DataInputStream(input);
                        int length = dataInputStream.readInt();
                        System.out.println("@@@@@@@@@@@@@@@@: " + port + "、length = " + length);
                        byte[] buffer = new byte[length];
                        dataInputStream.readFully(buffer);
                        String receivedMessage = new String(buffer);
                        System.out.println("@@@@@@@@@@@@@@@@: " + port + "、接收内容 = " + receivedMessage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                    InputStream inputStream = null;
                    OutputStream outputStream = null;
                    try {
                        inputStream = clientSocket.getInputStream();
                        outputStream = clientSocket.getOutputStream();
                        log.info(" ===== @@ 开始接收");
                        byte[] bytes = new byte[4096];
                        int len;
                        StringBuilder sb = new StringBuilder();

                        while ((len = inputStream.read(bytes)) != -1) {
                            log.info("before ====== @@@@@@: " + sb.toString());
                            sb.append(new String(bytes, 0, len));
                            log.info("end ====== @@@@@@: " + sb.toString());
                            String temp = sb.toString();
                            if (temp.indexOf("GG") > -1) {
                                log.info("@@@@ 跳出循环: " + temp);
                                break;
                            }
                        }
                        log.info("finish ====== @@@@@@: " + port + "、收到消息：" + sb.toString());
                        /*byte[] buffer = new byte[4096];
                        int bytesRead = inputStream.read(buffer);
                        String receivedMessage = new String(buffer, 0, bytesRead);
                        System.out.println("@@@@@: " + port + "、收到消息：" + receivedMessage);*/

                        // 发送响应数据给客户端
                        String responseMessage = port + "、已收到消息";
                        outputStream.write(responseMessage.getBytes());

                    } catch (Exception ex) {
                        ex.printStackTrace();
                        log.error(port + "处理数据异常信息：" + ex.getMessage());
                    } finally {
                        try {
                            // 关闭连接
                            clientSocket.close();
                            System.out.println("ServerSocket关闭成功");
                        } catch (Exception e) {
                            log.error("ServerSocket关闭错误：" + e);
                        }
                        try {
                            if (inputStream != null) {
                                inputStream.close();
                            }
                            if (outputStream != null) {
                                outputStream.close();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }).start();
    }
}
